package com.hh.vietnameseproject.ui.recite

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import com.hh.vietnameseproject.databinding.ActivityPlanDetailsBinding
import com.hh.vietnameseproject.databinding.ActivityReciteAtivityBinding
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.login.ActivityController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import kotlin.properties.Delegates

class ReciteAtivity : AppCompatActivity(), ViewPager.OnPageChangeListener {

    lateinit var binding: ActivityReciteAtivityBinding
    lateinit var cardViewPagerAdapter: CardViewPagerAdapter
    var id = 1
    var count = 20
    val list = ArrayList<CardInfo>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityReciteAtivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ActivityController.addList(this)
        id = intent.getIntExtra("id", 1)
        count = intent.getIntExtra("count",20)
        updateUI()
    }

    fun updateUI() {

        GlobalScope.launch {
            val body = AllNetWork.getWords("1")
            val jsonObject = JSONObject(body.string())
            val jsonArray = jsonObject.getJSONArray("data")
            for (i in 0 until count) {
                val word = jsonArray.getJSONObject(i)
                val sentence = word.getJSONArray("sentences").getJSONObject(0)
                val cardInfo = CardInfo(
                    word.getString("vnword"),
                    word.getString("cnword"),
                    word.getString("audio"),
                    word.getString("property"),
                    sentence.getString("vnsentence"),
                    sentence.getString("cnsentence")

                )
                list.add(cardInfo)

            }
            withContext(Dispatchers.Main) {
                cardViewPagerAdapter = CardViewPagerAdapter(supportFragmentManager, list)
                binding.reciteViewPager.adapter = cardViewPagerAdapter
            }


        }


        binding.reciteViewPager.offscreenPageLimit = 3
        binding.reciteViewPager.setPageTransformer(true, CardTransformer())
        binding.reciteViewPager.addOnPageChangeListener(this)

        binding.reciteBackBtn.setOnClickListener { finish() }
    }

    fun nextPage(position: Int){
        binding.reciteViewPager.setCurrentItem(position)
    }
    override fun onPageSelected(position: Int) {
        binding.showPositionText.text = "正在学习第${position+1}个单词"
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityController.removeList(this)
    }


}